import { ref } from "vue"
import { defineStore } from 'pinia'
import { inProgressStore } from "./inProgress"
import { createImagesAPI } from "@/api/index.js"

// 文生图
export const chatCreateImagesStore = defineStore('chatCreateImages', () => {
	const messages = ref([]) //存储图片url;prompt
	const inProgress = inProgressStore() // 对话是否进行中

	// 开始发送
	const seartSending = async (content) => {
		messages.value = [{ url: '', prompt: 'AI正在生成中...' }]
		inProgress.setStatus(true)
		try {
			const res = await createImagesAPI({ prompt: content })
			let aimessages = messages.value[0]
			if (res.serviceCode === 200) {
				aimessages.url = res.data.url
			} else {
				aimessages.url = ''
			}
			aimessages.prompt = res.data?.prompt || res.msg || '当前生成图片出错,请重试'
			inProgress.setStatus(false)
		} catch (err) {
			messages.value = [{ url: '', prompt: '当前生成图片出错,请重试' }]
			inProgress.setStatus(false)
		}
	}


	return { messages, seartSending }
})